import os
import json

def check_file_exists(file_name:str ):
    '''
    判断指定文件是否存在
    :param file_name:文件路径
    :return:
    '''
    if os.path.exists(file_name):
        return True
    else:
        return False



def make_folds(fold_path):
    '''
    创建文件路径
    :param fold_path:
    :return:
    '''
    os.makedirs(fold_path,exist_ok=True)



def load_files(file_path):
    '''
    加载文件夹里面的文件，并根据文件名里面的数字排序
    :param file_path:
    :return:
    '''
    files = []
    for file_name in os.listdir(file_path):
        files.append(os.path.join(file_path,file_name))

    files.sort(key=lambda x: int("".join(list(filter(str.isdigit, x)))))
    return files



def read_file(file_name,default_value =None,out_type='json'):
    if default_value != None:
        output = default_value
    else:
        output =  {}
    if os.path.exists(file_name):
        with open(file_name,'r',encoding='utf-8') as fread:
            if out_type == 'json':
                output = json.load(fread)

    return output


def write_file(file_name,input_str,input_type="json"):
    if input_str:
        with open(file_name, 'w', encoding='utf-8') as fwrite:
            if input_type == 'json':
                json.dump(input_str,fwrite,ensure_ascii=False,indent=4)
